package main

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"io/ioutil"
	"iot-base/common/logger"
)

func cmdreq(c *gin.Context) {
	req, _ := ioutil.ReadAll(c.Request.Body)
	fmt.Println(string(req))
	res := DealRes(string(req))
	vlue, ok := res["Return"]
	if !ok {
		c.Writer.Write([]byte("OK"))
		fmt.Println("return not zero!")
		return
	}
	if vlue == "0" {
		c.Writer.Write([]byte("OK"))
		return
	} else {
		Order = 0
		switch Order {
		case sendCard:
			fmt.Println("ID:", res["ID"])
			switch res["ID"] {
			case "10":
				logger.Log.Info("下发工号失败:" + string(req))
			case "11":
				logger.Log.Info("下发权限失败:" + string(req))
			case "12":
				logger.Log.Info("下发时间规则失败:" + string(req))
			}
			sc1.init()
			Order = 0
		case queryCard:
			logger.Log.Info("查询工号失败:" + string(req))
			Order = 0
		case deleteCard:
			switch res["ID"] {
			case "60":
				logger.Log.Info("删除权限失败:" + string(req))
			case "70":
				logger.Log.Info("删除pin卡失败:" + string(req))
			}
			Order = 0
			dc1.init()
		case openDoor:
			logger.Log.Info("远程开门失败:" + string(req))
			Order = 0
		case closeDoor:
			logger.Log.Info("远程关门失败:" + string(req))
			Order = 0
		}
	}
	fmt.Println("send cmd success:")
	c.Writer.Write([]byte("OK"))
}
